package com.jr.Filter1;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
//@WebFilter(urlPatterns = "/*")
public class Filter1_LoginFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        HttpServletResponse resp=(HttpServletResponse) servletResponse;
        String requestURI=req.getRequestURI();
        System.out.println(requestURI);
        if (requestURI.contains("login.jsp")||requestURI.contains("/UserController/")){
            filterChain.doFilter(req,resp);
            return;
        }
        HttpSession session=req.getSession();
        Object user=session.getAttribute("user");
        if (null!=user){
            filterChain.doFilter(req,resp);
        }else {
            resp.sendRedirect("login.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
